package com.sjz.han.industry.activites;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjz.han.industry.R;
import com.sjz.han.industry.base.BaseActivity;
import com.sjz.han.industry.entities.KeFuJsEntity;
import com.sjz.han.industry.entities.LoignEntity;
import com.sjz.han.industry.internetmanage.ReqestUrl;
import com.sjz.han.industry.utils.JsonUtils;
import com.sjz.han.industry.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * 产品介绍
 */
public class KeFuJsActivity extends BaseActivity implements View.OnClickListener{
    private WebView mWebView;
    private ProgressBar progress_wheel;
    @Override
    protected int setLayoutId() {
        return R.layout.activity_kefujs;
    }

    @Override
    protected void initDatas() {
     getKeFu();
    }

    @Override
    protected void initViews() {
        reback_btn=(RelativeLayout)this.findViewById(R.id.reback_btn);
        titleTv=(TextView)this.findViewById(R.id.ctitle_tv);
        titleTv.setText("客服");

        mWebView=(WebView)this.findViewById(R.id.wv_contnet);
        progress_wheel=(ProgressBar)this.findViewById(R.id.progress_wheel);
    }

    @Override
    protected void setLisener() {
        reback_btn.setOnClickListener(this);
    }

    @Override
    protected void onRetry() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.reback_btn:
                finish();
                break;
        }
    }

    private void setWebViewSetting(String content) {
        mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setAppCacheEnabled(true);
        mWebView.getSettings().setAllowFileAccessFromFileURLs(true);
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setVerticalScrollbarOverlay(false);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.setHorizontalScrollbarOverlay(false);
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        mWebView.getSettings().setDomStorageEnabled(true);
//        mWebView.loadUrl("http://www.baidu.com");
        mWebView.loadDataWithBaseURL(null, content,
                "text/html", "utf-8", null);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                progress_wheel.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
//                SysUtils.imgReset(mWebView);
                progress_wheel.setVisibility(View.GONE);
            }
        });
    }

    public void getKeFu(){

        OkHttpUtils
                .post()
                .url(ReqestUrl.KEFU_JS_URL)
                .addParams("token",shareUtils.getToken())
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtils.popUpToast("请求超时");
            }
            @Override
            public void onResponse(String response, int id) {
                if(ReqestUrl.rebacRequestJson(response)!=null){
                    KeFuJsEntity entity= JsonUtils.getObject(response,KeFuJsEntity.class);
                    if(entity!=null&&entity.code==0){
                        if(entity.data.customer!=null){
                            setWebViewSetting(entity.data.customer.content);
                        }

                    }else{
                        ToastUtils.popUpToast(entity.msg);
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }
}
